/* 
 * File:   Inv.cpp
 * Author: Gab'z
 * 
 * Created on 1 juin 2012, 22:15
 */

#include "Inv.h"
#include "../../../Context.h"
#include "../../literal/Stack.h"
#include "../../literal/Complex.h"
#include "../../literal/Integer.h"
#include "../../literal/Rationnal.h"

namespace calculator {

    Inv::Inv(const std::string name) : UnaryOperator(name) {
    }

    Inv::Inv(const Inv& orig) : UnaryOperator(orig._name) {
    }

    Inv::~Inv() {
    }

    Inv* Inv::clone() const {
        return new Inv(_name);
    }

    const Number* Inv::apply(const Number* number) const throw (ArithmeticException) {


        const Number* numResult;



        const Integer* integer = dynamic_cast<const Integer*> (number);
        if (integer != 0) {
            numResult = new Rationnal(Rationnal(1) / *integer);
        } else {
            const Number* tmpNumber = number->clone();
            const Rationnal* unite = new Rationnal(1, 1);
            try {
                numResult = div(unite, tmpNumber);
            } catch (ArithmeticException& e) {
                delete tmpNumber;
                throw;
            }
            delete tmpNumber;
        }
        return numResult;

    }

}